'''
Desc: QLineEdit综合实例
Author: 陈爱国
Date: 2024-08-26 14:25:31
'''
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QFormLayout
from PyQt5.QtGui import QIntValidator, QFont, QDoubleValidator
from PyQt5.QtCore import Qt
import sys

class QLineEditDemo(QWidget):
    def __init__(self):
        super(QLineEditDemo, self).__init__()
        self.initUI()

    def initUI(self):
        edit1 = QLineEdit()
        # 使用int校验器
        edit1.setValidator(QIntValidator())
        # 设置最大数字长度
        edit1.setMaxLength(4)
        # 右对齐
        edit1.setAlignment(Qt.AlignRight)
        # 设置字体
        # edit1.setFont(QFont('Arial', 20))

        # 浮点数
        edit2 = QLineEdit()
        edit2.setValidator(QDoubleValidator(0.99, 99.99, 2))

        # 掩码
        edit3 = QLineEdit()
        edit3.setInputMask('99_999_9999;#')

        # 输入内容检测
        edit4 = QLineEdit()
        edit4.textChanged.connect(self.textChanged)

        # 回显
        edit5 = QLineEdit()
        edit5.setEchoMode(QLineEdit.Password)
        edit5.editingFinished.connect(self.enterPress)

        # 只读文本
        edit6 = QLineEdit("Hello PyQt5")
        edit6.setReadOnly(True)

        # 设置布局
        formLayout = QFormLayout()
        formLayout.addRow("整数校验", edit1)
        formLayout.addRow("浮点数校验", edit2)
        formLayout.addRow("Input Mask", edit3)
        formLayout.addRow("文本变化", edit4)
        formLayout.addRow("密码", edit5)
        formLayout.addRow("只读", edit6)

        # 设置窗体的布局
        self.setLayout(formLayout)

        # 设置窗口标题
        self.setWindowTitle('QLineEdit综合实例')

    def textChanged(self, text):
        """
        文本改变的槽
        """
        print("输入的文本："+text)

    def enterPress(self):
        print('输入结束')


if __name__ == '__main__':
    app = QApplication(sys.argv)

    win = QLineEditDemo()
    win.show()

    sys.exit(app.exec_())




